package com.yyplatform.admin.platform.schedule.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.schedule.model.dto.JobLogDTO;
import com.yyplatform.admin.platform.schedule.model.dto.JobLogQueryDTO;
import com.yyplatform.admin.platform.schedule.service.JobLogService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/21 22:49
 */
@Slf4j
@Tag(name = "任务/任务日志管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/schedule/job/log/")
public class JobLogController {

    private JobLogService jobLogService;


    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:jobLog:query")
    public RestResponse<Page<JobLogDTO>> page(JobLogQueryDTO pageQueryDTO) {
        log.debug("query dto is {}", pageQueryDTO);

        Page<JobLogDTO> page = jobLogService.query(pageQueryDTO);

        return RestResponse.ok(page);
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:jobLog:get")
    public RestResponse<JobLogDTO> get(Long id) {
        log.debug("get by id is {}", id);

        JobLogDTO data = jobLogService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:jobLog:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);

        jobLogService.delete(id);
        return RestResponse.ok(null);
    }


    @PostMapping("deleteAll")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:jobLog:deleteAll")
    public RestResponse<Void> deleteAll(Long jobId) {
        log.debug("deleteAll job log by jobId is {}", jobId);


        jobLogService.deleteByJobId(jobId);
        return RestResponse.ok(null);
    }


}
